package com.gx.smart.building.ui.warning

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import android.view.ViewGroup
import com.blankj.utilcode.util.ToastUtils
import com.gx.smart.base.BaseMVVMActivityV3
import com.gx.smart.building.R
import com.gx.smart.building.databinding.ActivityWarningSubmitBinding
import com.gx.smart.building.ui.warning.event.WarningMessageEvent
import com.gx.smart.building.ui.warning.viewmodel.WarningViewModel
import com.gx.smart.lib.http.api.response.MessageAlarm
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class WarningSubmitActivity : BaseMVVMActivityV3<WarningViewModel>() {
    private lateinit var viewBinding: ActivityWarningSubmitBinding
    private lateinit var detail: MessageAlarm
    private var dealType: Int = 0
    override fun onBindViewModel(): Class<WarningViewModel> = WarningViewModel::class.java

    override fun onBindLayout(): Int = R.layout.activity_warning_submit

    override fun getToolbarTitle(): String = "告警处理"

    override fun initContentView(root: ViewGroup) {
        viewBinding = ActivityWarningSubmitBinding.inflate(layoutInflater, root, true)
        dealType = intent.getIntExtra("dealType", 0)
        viewBinding.dealType.text = when (dealType) {
            1 -> "转工单" //永远不会是1
            2 -> "误报消除"
            3 -> "已解决"
            4 -> "忽略"
            else -> ""
        }
        viewBinding.dealDesc.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                viewBinding.wordCount.text = s.length.toString() + "/200"
            }
        })

        viewBinding.save.setOnClickListener {
            save()
        }
    }

    fun save() {
        detail.dealType = dealType
        val dealDesc = viewBinding.dealDesc.text.toString().trim()
        detail.dealDesc = dealDesc

//        viewModel.deal(detail.build()).observe(this) {
//            if (it) {
                ToastUtils.showLong("提交成功")
                startActivity(Intent(this, WarningResultActivity::class.java))
                finish()
//            }
//        }
    }


    override fun initView() {
        registerEventBus()
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterEventBus()
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    fun handleEvent(event: WarningMessageEvent) {
        detail = event.messageAlarm
    }

}